package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.zonglzp.entity.Customer;
import org.jeecg.modules.zonglzp.entity.ExcelEntity;
import org.jeecg.modules.zonglzp.service.CompanyService;
import org.jeecg.modules.zonglzp.service.CustomerService;
import org.jeecg.modules.zonglzp.service.IBaseService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CompanyService companyservice;

    @Autowired
    CustomerService service;

    @Autowired
    static
    IBaseService baseService;

    /*
    查询客户列表
     */
    @GetMapping("/getCustomerlist")
    public JSONObject getCustomerlist(
            @RequestParam(name = "token") String token,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String customerCode,  //客户编号
            String customerName  //客户名称
    ) {

        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = companyservice.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }

        Page<Customer> page = new Page<Customer>(pageNo, pageSize);
        IPage<Customer> pageList = service.getCustomerlist(page, userid, customerCode, customerName);
        if (pageList.getRecords().size() > 0) {
            obj.put("code", 0);
            obj.put("data", pageList);
        } else {
            obj.put("code", 1);
            obj.put("data", "暂无数据");
        }

        return obj;
    }


    /*
    根据员工编号查询员工
     */
    @GetMapping("/getyuangongbycode")
    public JSONObject getyuangongbycode(
            String customerCode
    ) {
        JSONObject obj = new JSONObject();

        Customer yuangong = service.getyuangongbycode(customerCode);

        if (yuangong == null) {
            obj.put("code", 1);
            obj.put("data", "没有数据");
        } else {
            obj.put("code", 0);
            obj.put("data", yuangong);
        }

        return obj;
    }

    /*
     查询未量体的员工
     */
    @GetMapping("/getweiliangti")
    public JSONObject getweiliangtiyuangong(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String departId
    ) {
        JSONObject obj = new JSONObject();
        Page<Customer> page = new Page<Customer>(pageNo, pageSize);
        IPage<Customer> pageList = service.getweiliangtiyuangong(page, departId);
        if (pageList != null && pageList.getRecords().size() > 0) {
            obj.put("code", 0);
            obj.put("data", pageList);
        } else {
            obj.put("code", 1);
            obj.put("data", "暂无数据");
        }
        return obj;
    }


    /*
    设置花名册
     */
    @PutMapping("sethmc")
    public JSONObject sethmc(
            List<Customer> settitles,
            String departId2
    ) {
        JSONObject obj = new JSONObject();

        //先删除老的花名册里的东西
        service.deletedeparthmc(departId2);

        //设置花名册往里插数据
        for (int i = 0; i < settitles.size(); i++) {
            service.sethmc(settitles.get(i), departId2);
        }
        return obj;
    }


    /*
    查询花名册的员工
     */
    @GetMapping("/gethmclist")
    public JSONObject gethmclist(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String departId
    ) {
        JSONObject obj = new JSONObject();

        //这里先查询标题内容
        //  List<Customer> titles=service.gettitles(departId);

        Page<Customer> page = new Page<Customer>(pageNo, pageSize);
        IPage<Customer> pageList = service.gethmclist(page, departId);

       int womenNumber=service.getsexNumber(departId,2);

       int manNumber=service.getsexNumber(departId,1);

     int sumNumber=womenNumber+manNumber;


        if (pageList!=null&&pageList.getRecords().size() > 0) {
            obj.put("code", 0);
               obj.put("womenNumber",womenNumber);
            obj.put("manNumber",manNumber);
            obj.put("sumNumber",sumNumber);
            obj.put("data", pageList);
        } else {
            obj.put("code", 1);
            obj.put("womenNumber",womenNumber);
            obj.put("manNumber",manNumber);
            obj.put("sumNumber",sumNumber);
            obj.put("data", "暂无数据");
        }
        return obj;
    }


    /*
   花名册中的 删除员工
     */
    @GetMapping("/deleteCustomer")
    public JSONObject deleteCustomer
    (
            int id
    ) {
        JSONObject obj = new JSONObject();
        //删除员工吧  应该是假删除
        int count = service.deleteCustomer(id);

        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "删除成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "删除失败");
        }
        return obj;
    }


    /*
    花名册中的编辑员工
     */
    @PostMapping("/updateCustomer")
    public JSONObject updateCustomer(
            Customer customer
    ) {
        JSONObject obj = new JSONObject();
        int count = service.updateCustomer(customer);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "修改成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "修改失败");
        }
        return obj;
    }


    /*
    导入excel
     */
    @PostMapping("/importExcel")
    public JSONObject importExcel(MultipartFile myFileNames,
                                  String departId,
                                  int parentId) throws Exception {
        JSONObject obj = new JSONObject();
//        try{
          System.out.println("在逗我吗"+departId);
        if("null".equals(departId)||departId==null||"".equals(departId)){
            obj.put("code", 1);
            obj.put("data", "未选择部门");
            return obj;
        }

        ImportParams params = new ImportParams();
        //表格标题所在行，计数从0开始
        params.setTitleRows(1);
        //head头部所在行，计数从0开始
        params.setHeadRows(1);
        //表格sheet数量
        //params.setSheetNum(9);
        // 最好不要设置为true，否则导入一次excel服务器会重启一次，原因不明
        //params.setNeedSave(true);
        InputStream inputStream = myFileNames.getInputStream();
        List<ExcelEntity> list = ExcelImportUtil.importExcel(inputStream, ExcelEntity.class, params);

        //导入的excel进行循环一条条插入到表中
        for (int i = 0; i < list.size(); i++) {
            service.importhmc(list.get(i), departId,parentId);
        }
        obj.put("code", 0);
        obj.put("data", list);
        return obj;
//        }catch (Exception e){
//            e.printStackTrace();
//            obj.put("code", 1);
//            obj.put("data", "导入出错");
//            return obj;
//        }
    }



    /*
    花名册中下载模板，模板中的数据
     */

    public List<ExcelEntity> mobandata() {
        List<ExcelEntity> list = new ArrayList<ExcelEntity>();
        list = service.getmobandata();
        return list;
    }

    /*
     *  设置花名册地方的下载模板
     */
    @GetMapping("/downloadmoban")
    //使用jeecgBoot框架提供的导出功能，导出用工企业列表
    public ModelAndView downloadqyrzsh(HttpServletRequest request, HttpServletResponse response
    ) {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "花名册列表模板");
        //注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, ExcelEntity.class);
        //自定义表格参数
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("花名册模板", "花名册模板文件"));
        //导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST, mobandata());
        return mv;
    }

    /*
    清空部门
     */
    @GetMapping("/cleardepart")
    public JSONObject cleardepart(
            String departId
    ) {
        JSONObject obj = new JSONObject();

        int count = service.cleardepart(departId);
        if (count > 0) {
            obj.put("data", "清空成功");
            obj.put("code", 0);
        } else {
            obj.put("data", "清空失败");
            obj.put("code", 1);
        }
        return obj;
    }
}
